package 
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;

import mx.controls.Alert;

import mx.events.FlexEvent;
	import mx.preloaders.DownloadProgressBar;
	
	public class CustomPreloader extends DownloadProgressBar 
	{

	    [Embed(source="start1.swf", symbol="Preloader")]
	    public var WelcomeScreenGraphic:Class;
	    
	    public var wcs:MovieClip;

		public function CustomPreloader():void
		{
			super(); 
            wcs = new WelcomeScreenGraphic();
            addChild(wcs); 
            var w:Number = new Number(0);
            var h:Number = new Number(0);
          
            wcs.gotoAndStop(0);
		}
		
        public override function set preloader(preloader:Sprite):void 
        {                   
            preloader.addEventListener(ProgressEvent.PROGRESS, onSWFDownloadProgress);    
            preloader.addEventListener(Event.COMPLETE, onSWFDownloadComplete);
            preloader.addEventListener(FlexEvent.INIT_PROGRESS, onFlexInitProgress);
            preloader.addEventListener(FlexEvent.INIT_COMPLETE, onFlexInitComplete);
            
            centerPreloader();
        }	
        private function centerPreloader():void
        {
            x = (stageWidth / 2) - (wcs.width / 2);
            y = (stageHeight / 2) - (wcs.height / 2);
        }
        private function onSWFDownloadProgress(event:ProgressEvent):void
        {
            var t:Number = event.bytesTotal;
            var l:Number = event.bytesLoaded;
            var p:Number = Math.round((l/t) * 19);
            var pForAmount:int = Math.floor(p * 5);
            wcs.gotoAndStop(p);
            //wcs.amount_txt.text = String(pForAmount) + "%";
        }
        private function onSWFDownloadComplete(event:Event):void
        {
           // wcs.gotoAndStop(100);
          //  wcs.amount_txt.text = "100%";

        }
        private function onFlexInitProgress(event:FlexEvent):void
        {
           // wcs.gotoAndStop(100);
           // wcs.amount_txt.text = " ";
        }
        private function onFlexInitComplete(event:FlexEvent):void 
        {
           // wcs.gotoAndStop(100);
            dispatchEvent( new Event(Event.COMPLETE));
        }
	}
}